package com.chzc.fm.duotin.adapter.recyclerview;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.chzc.fm.duotin.R;
import com.chzc.fm.duotin.bean.channel.ChannelType;
import com.chzc.fm.duotin.widget.MyItemTouchCallback;

import java.util.Collections;
import java.util.List;

/**
 * Created by Administrator on 2016/7/1.
 */
public class EditTypeAdapter extends RecyclerView.Adapter<EditTypeAdapter.EditTypeViewHolder> implements MyItemTouchCallback.ItemTouchAdapter {

    private Context context;
    private List<ChannelType> typeList;
    private LayoutInflater inflater;

    public EditTypeAdapter(Context context, List<ChannelType> typeList) {
        this.context = context;
        this.typeList = typeList;

        inflater = LayoutInflater.from(context);
    }

    @Override
    public EditTypeViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = inflater.inflate(R.layout.adapter_edittype,null);
        return new EditTypeViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(EditTypeViewHolder holder, int position) {

        holder.tvtitle.setText(typeList.get(position).getTitle().toString());
    }

    @Override
    public int getItemCount() {
        return typeList == null ? 0 : typeList.size()-1;
    }

    @Override
    public void onMove(int fromPosition, int toPosition) {

        if (fromPosition < toPosition) {
            for (int i = fromPosition; i < toPosition; i++) {
                Collections.swap(typeList, i, i + 1);
            }
        } else {
            for (int i = fromPosition; i > toPosition; i--) {
                Collections.swap(typeList, i, i - 1);
            }
        }
        notifyItemMoved(fromPosition, toPosition);
    }

    @Override
    public void onSwiped(int position) {

        typeList.remove(position);
        notifyItemRemoved(position);
    }

    public class EditTypeViewHolder extends RecyclerView.ViewHolder {

        TextView tvtitle;
        public EditTypeViewHolder(View itemView) {
            super(itemView);

            tvtitle  = (TextView) itemView.findViewById(R.id.adapter_edittype_tv);
        }
    }
}
